﻿Imports System.Windows.Forms
Imports System.Text
Imports System.IO
Imports CRM.BusinessLayer
Partial Public Class frmLoadToDocStore

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim OpenFileDialog1 As New OpenFileDialog

        OpenFileDialog1.FileName = String.Empty

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            'Dim myItem As New DataBinding.MyListItem

            'myItem.ItemText = Me.OpenFileDialog1.FileName

            Me.lstFiles.Items.Add(OpenFileDialog1.FileName)

        End If
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        Me.lstFiles.Items.Remove(Me.lstFiles.SelectedItem)

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.lstFiles.Items.Clear()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim Tsk As New TasksEvents
        Dim DocStoreId As Long
        Dim Msg As New StringBuilder
        Dim myItem As Object
        Dim myFile As FileInfo
        Try

        
            If Me.txtStoreName.Text.Trim = String.Empty Then
                CommonFuncs.DisplayMessage("You must provide a name for this document store", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtStoreName.Focus()
                Exit Sub
            End If

            If Me.lstFiles.Items.Count = 0 Then
                CommonFuncs.DisplayMessage("No files to add!", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            DocStoreId = Letters.GblFunc_Create_DocStore(Me.txtStoreName.Text, "Loaded by consultant", GlobalCRM.CurrentCaseID)

            Msg.Append("Document store '" & Me.txtStoreName.Text & "' created and the following documents uploaded" & vbCrLf & vbCrLf)

            For Each myItem In Me.lstFiles.Items

                If File.Exists(myItem.ToString) Then


                    myFile = New FileInfo(myItem.ToString)

                    Letters.GblFunc_Add_FileToStore(myFile.FullName, myFile.Name, DocStoreId)


                    Msg.Append(myFile.FullName & vbCrLf)

                Else
                    CommonFuncs.DisplayMessage("File '" & myItem.ToString & "' could not be found. The document store will be created, but this file has not been loaded", CommonDialog.CustomDialogIcons.Information, "Error")


                End If

            Next

            Tsk.GblFunc_Add_Event("Documents loaded", Msg.ToString, TasksEvents.EventStatus.SingleEvent, GlobalCRM.CurrentCaseID, TasksEvents.EventTypes.EventCase)

            'Call GblFunc_Populate_Case_Labels()

            Me.Close()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmLoadToDocStore : btnSave")
        Finally
            Tsk = Nothing
            Msg = Nothing
            myFile = Nothing
        End Try

    End Sub

    Private Sub frmLoadToDocStore_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If

    End Sub

    Private Sub frmLoadToDocStore_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
    End Sub
End Class
